import Jwt from "../../prototype/jwt";
import admin_menu from "../../models/admin_menu";
class Menu_management extends Jwt{
  constructor() {
    super();
    this.get_tree = this.get_tree.bind(this);
  }

  set_tree(arr, data, p_label) {
    let list = [];
    let i = 0;
    arr.map(el => {
        if(el.p_label === p_label) {
          list[i] = el;
          i++;
          // children
          this.set_tree(arr, el, el.label);
        }
    })

    data.children = list;
  }

  async get_tree (req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    }

    try {
      const list = await admin_menu.find({});
      const data = list.filter(el => el.p_label === '0' && el);
      data.forEach(el => {
        this.set_tree(list, el, el.label)
      })

      body.data = data;


    } catch (e) {
      body.status = 201
      body.msg = e.toString();
    }

    res.send(body)
  }

  do_edit (req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    }
    res.send(body)
  }

  do_delete(req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    }
    res.send(body)
  }

}


export default Menu_management
